package org.inter.fsb.controller;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.inter.fsb.entity.TUserWord;
import org.inter.fsb.entity.TWork;
import org.inter.fsb.entity.User;
import org.inter.fsb.service.ITUserWordService;
import org.inter.fsb.service.ITWorkService;
import org.inter.fsb.service.IUserService;
import org.inter.fsb.utils.ProxyResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.inter.fsb.controller.BaseController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author syg
 * @since 2024-04-18
 */
@Controller
@RequestMapping("/inter/t-work")
public class TWorkController extends BaseController {

    @Autowired
    private ITWorkService service;

    @Autowired
    private IUserService iUserService;
    @Autowired
    private ITUserWordService itUserWordService;

    @ApiOperation("查询一个功能")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public @ResponseBody
    ProxyResponse get(TWork entity) {
        Wrapper<TWork> wrapper = new QueryWrapper<TWork>(entity);
        TWork result = service.getOne(wrapper);
        return toJson(result);
    }

    @ApiOperation("查询一个功能")
    @RequestMapping(value = "/get_workg", method = RequestMethod.GET)
    public @ResponseBody
    ProxyResponse getWorkg(TWork entity) {
        Wrapper<TWork> wrapper = new QueryWrapper<TWork>(entity);
        TWork result = service.getOne(wrapper);
        QueryWrapper<TUserWord> tUserWordQueryWrapper = new QueryWrapper<>();
        tUserWordQueryWrapper.eq("work_id",result.getId());
        List<TUserWord> list = itUserWordService.list(tUserWordQueryWrapper);
        result.setWordList(list);
        return toJson(result);
    }

    @ApiOperation("查询一个我领取的功能")
    @RequestMapping(value = "/get_myl", method = RequestMethod.GET)
    public @ResponseBody
    ProxyResponse getMyL(Integer id) {
        TUserWord byId = itUserWordService.getById(id);

        return toJson(byId);
    }

    @ApiOperation("查询功能")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public @ResponseBody
    ProxyResponse list(@RequestParam(value = "paixv", defaultValue = "0", required = false) int paixv
            , @RequestParam(value = "sou", required = false) String sou
    ) {
        QueryWrapper<TWork> tWorkQueryWrapper = new QueryWrapper<>();

        if (sou != null){
            tWorkQueryWrapper.like("title",sou);
        }

        if (paixv == 0){
            tWorkQueryWrapper.orderByDesc("date_time");
        }else {
            tWorkQueryWrapper.orderByDesc("jine");
        }

        List<TWork> result = service.list(tWorkQueryWrapper);
        return toJson(result);
    }


    @ApiOperation("新建或更新数据功能")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public @ResponseBody ProxyResponse save(@RequestBody TWork entity) throws Exception {

        if (entity.getUsername() != null) {

            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("username", entity.getUsername());
            User one = iUserService.getOne(userQueryWrapper);
            if (one != null) {
                entity.setUserId(one.getId());
                entity.setDateTime(DateUtil.format(new Date(), "yyyy-MM-dd"));
                entity.setChushigeshu(entity.getGeshu());
                boolean result = service.saveOrUpdate(entity);
                if (result) {
                    return toJson("保存成功");
                }
            } else {
                throw new Exception("格式错误");
            }

        } else {
            throw new Exception("格式错误");
        }

        throw new Exception("格式错误");

    }


    /**
     *  任务任务操作相关
     */

    @ApiOperation("抢任务")
    @RequestMapping(value = "/q_work", method = RequestMethod.GET)
    public @ResponseBody
    ProxyResponse Qwork(@RequestParam(value = "username",required = true)String username,
                        @RequestParam(value = "workId",required = true) Integer workId) throws Exception {

        service.qwork(username,workId);

        return toJson(null);
    }

    @ApiOperation("提交任务")
    @RequestMapping(value = "/t_work", method = RequestMethod.GET)
    public @ResponseBody
    ProxyResponse Twork(@RequestParam(value = "zhengming",required = true)String zhengming,
                        @RequestParam(value = "userWorkId",required = true) Integer userWorkId) throws Exception {

        service.twork(zhengming,userWorkId);

        return toJson(null);
    }

    @ApiOperation("和我相关所有任务列表 1:我发布的  2:已完成 3：待审核  4：已审核")
    @RequestMapping(value = "/list_work", method = RequestMethod.GET)
    public @ResponseBody
    ProxyResponse ListWork(@RequestParam(value = "username",required = true)String username,
                        @RequestParam(value = "status",required = true) Integer status) throws Exception {

        List list = service.listWork(username, status);

        return toJson(list);
    }

    @ApiOperation("审核  0:拒绝  1：通过")
    @RequestMapping(value = "/shen", method = RequestMethod.GET)
    public @ResponseBody
    ProxyResponse shen(@RequestParam(value = "uwId",required = true) Integer uwId,
                           @RequestParam(value = "status",required = true) Integer status) throws Exception {

        service.shen(uwId,status);

        return toJson(null);
    }
}






























